import { getTokenByCode } from './assets/js/api.js';
App({
  code: '',
  employee: {
    name: '',
    mobile: '',
    jobNum: ''
  },
  getEmployeeInfo() {
    return new Promise((resolve, reject) => {
      if (this.employee.name) resolve(this.employee);
      let params = {
        code: this.code
      }
      getTokenByCode(params).then(res => {
        if (res.code === 0) {
          const token = res.data.access_token;
          const employee = res.data.info_detail.employeeInfo;
          this.employee.name = employee.name;
          this.employee.mobile = employee.mobile;
          this.employee.jobNum = employee.jobNum;
          my.setStorageSync({
            key: 'token',
            data: token
          })
          resolve(this.employee);
        } else {
          reject(res);
        }  
      }).catch(err => {
        reject(err);
      });    
    })
  },
  onLaunch(options) {
    this.code = options.query.code || '';
  }
});
